package angry.courses.servlets;

import angry.courses.database.ICoursesDB;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import javax.servlet.ServletException;

public class DeleteServlet extends HttpServlet {
    private ICoursesDB coursesDB;

    public void init() throws ServletException {
	coursesDB = (ICoursesDB) getServletContext().getAttribute("coursesDB");
    }

    public void destroy() {
	coursesDB = null;
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
	final String name = req.getParameter("name");
	
	coursesDB.deleteStudent(name);

	res.setContentType("text/html");
	PrintWriter out = res.getWriter();
	out.println(new HtmlFormatter() {
	    public String title() {
		return "Registration Deleted";
	    }

	    public void body(StringBuffer buf) {
		buf.append(heading(3, "Completed"));
		buf.append("Registration of <b>" + name + "</b> deleted.<br>");
		buf.append("Click " + link("here", "index.html") +
			   " to go back.");
	    }
	}.htmlTag());
	out.close();
    }
}
